package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.OrganizationDao;
import com.jk.pojo.OrganizationBean;
import com.jk.utils.ReturnMsgUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jisiqi
 * @version 1.0
 * @description: TODO
 * @date 2022/10/14 14:21
 */
@Service
public class OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    RestHighLevelClient client;

    /**
     * @description:  查询机构管理
     * @param: organizationBean
     * @return: java.util.Map
     * @author 纪思齐
     * @date: 2022/10/14 14:27
     */
    public Map queryOrganizationList() {
        try {
            QueryWrapper<OrganizationBean> Wrapper = new QueryWrapper<OrganizationBean>();
            List<OrganizationBean> list = organizationDao.selectList(Wrapper);
            System.out.println("======="+list);
            if (list.size()>0){
                list.forEach(OrganizationBean ->{
                    elasticsearchRestTemplate.save(OrganizationBean);
                });
            }
            return ReturnMsgUtils.returnMsg(200,"查询MySQL机构管理,新增es",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    /**
     * @description: es查询机构管理+高亮
     * @param: organizationBean
     * @return: java.util.Map
     * @author 纪思齐
     * @date: 2022/10/14 15:29
     */
    public Map findEsList(OrganizationBean organizationBean) {
        try {
            List<OrganizationBean> list = new ArrayList<>();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //机构名称迷糊查
            if (organizationBean.getOrganizationName()!=null && organizationBean.getOrganizationName()!=""){
                boolQueryBuilder.must(QueryBuilders.matchQuery("organizationName",organizationBean.getOrganizationName()));
            }
            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(boolQueryBuilder)
                    .withHighlightFields(new HighlightBuilder.Field("organizationName").preTags("<font color='red'>").postTags("</font>"))
                    .build();
            SearchHits<OrganizationBean> search = elasticsearchRestTemplate.search(build, OrganizationBean.class);
            search.forEach(hit ->{
                OrganizationBean content = hit.getContent();
                Map<String, List<String>> highlightFields = hit.getHighlightFields();
                hit.getContent().setOrganizationName(highlightFields.get("organizationName")==null? hit.getContent().getOrganizationName():highlightFields.get("organizationName").get(0));
                list.add(content);
            });
            return ReturnMsgUtils.returnMsg(200,"es中机构管理数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    /**
     * @description: 根据机构管理的id删除
     * @param: id
     * @return: boolean
     * @author 纪思齐
     * @date: 2022/10/14 15:41
     */
    public boolean deleteOrganizationById(Integer id) {
        try {
//            organizationDao.deleteById(id);
            elasticsearchRestTemplate.delete(id.toString(), IndexCoordinates.of("organization"));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean changeState(Integer organizationId, Integer organizationState) {
        try {
            //修改MySQL中机构管理表的状态
            OrganizationBean organizationBean = new OrganizationBean();
            organizationBean.setOrganizationId(organizationId);
            organizationBean.setOrganizationState(organizationState);
            organizationDao.updateById(organizationBean);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
